/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.apli.haiam.servlet;

import com.apli.controladores.AplinfoFactoryBean;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apli.haiam.ejb.interfaces.AdministradorHaiam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet que permite actua en el autologin. 
 *
 * <H6>Soporte:HAIAM
 * <I>david_s_1086@hotmail.com,david.carranza@haiam.gob.ec</I></H6>
 *
 * @author david Carranza
 * @version 1.0 30/07/2014
 */
@WebServlet(name = "ServletImagen", urlPatterns = {"/ServletImagen"})
public class ServletImagen extends HttpServlet {

    protected AdministradorHaiam admin;
    final Logger log = LoggerFactory.getLogger(ServletImagen.class);
    private String _SIS;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String nombreMetodo = Thread.currentThread().getStackTrace()[1].getMethodName() + "(...)";
        log.debug("{}: Cargando imagen", nombreMetodo);

        response.setContentType("image/gif");
        OutputStream out = response.getOutputStream();
        try {
            cargarEjb();

            String _tabla = request.getParameter("tabla");
            String _campoClave = request.getParameter("campoClave");
            String _valorClave = request.getParameter("valorClave");
            String _campoImagen = request.getParameter("campoImagen");
            if (_tabla == null || _campoClave == null || _valorClave == null || _campoImagen == null) {
                return;
            }
            try {
                String sql = "select " + _campoImagen + " from " + _tabla + " where " + _campoClave + " = " + _valorClave;
                Object obj = admin.ejecutarQueryNativo(sql);
                byte[] imagen = (byte[]) obj;
                if (imagen != null) {
                    if (imagen != null) {
                        out.write(imagen);
                    }
                }
            } catch (Exception e) {
                log.error("{}: {}", nombreMetodo, e.getMessage());
            }
        } finally {
            out.close();
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    /**
     * Obtiene el objeto remoto.
     */
    protected void cargarEjb() {
        if (admin == null) {
            String ejbPath = null;
            try {
                ResourceBundle property = getProperties();
                String ORBContext = property.getString("ORBContext");
                String ejbContextPath = property.getString("ejb.context.path.admin");
                ejbPath = ORBContext + ejbContextPath + "/AdministradorBeanHaiam";
                admin = (AdministradorHaiam) AplinfoFactoryBean.lookupBean(ejbPath, false);
            } catch (Exception e) {
                log.error(_SIS + " cargarEjb() {} - {} ", ejbPath, e.getMessage());
            }
        }
    }

    protected ResourceBundle getProperties() {
        return ResourceBundle.getBundle("aplicacion");
    }
}
